package com.timmy.blekit.presenter;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.widget.Toast;

import com.inuker.bluetooth.library.search.SearchResult;
import com.timmy.blekit.callback.BleConnectCallback;
import com.timmy.blekit.callback.BleScanCallback;
import com.timmy.blekit.ui.activitys.MainActivity;
import com.timmy.blekit.ui.activitys.ShakeActivity;
import com.timmy.blekit.utils.BleUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Timmy
 * @Data: 2017/8/25
 * @Fuction:
 */

public class MainActivityPresenter {

    private MainActivity mMainActivity;
    private BleUtil mBleUtil;
    private List<SearchResult> mResults = new ArrayList<>();

    public MainActivityPresenter(MainActivity mainActivity) {
        mMainActivity = mainActivity;
    }

    public void initBlueTooth() {

        mBleUtil = BleUtil.getInstance();

        boolean isSupport = mBleUtil.isBleSupported();
        if (!isSupport) {
            Toast.makeText(mMainActivity, "此设备不支持ble蓝牙设备", Toast.LENGTH_SHORT).show();
            return;
        }
    }

    public void openBluetooth() {
        // 请求打开 Bluetooth
        Intent requestBluetoothOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        // 设置 Bluetooth 设备可以被其它 Bluetooth 设备扫描到
        requestBluetoothOn.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);

        // 设置 Bluetooth 设备可见时间
        requestBluetoothOn.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mMainActivity.BLUETOOTH_DISCOVERABLE_DURATION);

        // 请求开启 Bluetooth
        mMainActivity.startActivityForResult(requestBluetoothOn, mMainActivity.REQUEST_CODE_BLUETOOTH_ON);
    }

    public void scanBluetoothDevice() {
        mMainActivity.showProgressDialog("扫描蓝牙", "扫描中...");

        mBleUtil.scanBleDevice(new BleScanCallback() {
            @Override
            public void bleScanStart() {
            }

            @Override
            public void bleScanStop() {

                mMainActivity.hideProgressDialog();
                if (mResults.size() == 0) {
                    Toast.makeText(mMainActivity, "没有搜索到蓝牙设备", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void bleDevice(SearchResult device) {
                if (device.getName().contains("MoLi")) {
                    if (device.getAddress().contains("DF")) {
                        if (!mResults.contains(device)) {
                            mMainActivity.mDeviceAdapter.addData(device);
                            mResults.add(device);
                        }
                    }
                }
            }
        });
    }

    public void connectDevice(SearchResult result) {
        mBleUtil.connectDevice(result.getAddress(), new BleConnectCallback() {
            @Override
            public void bleConnectSucc() {
                Toast.makeText(mMainActivity, "连接成功", Toast.LENGTH_SHORT).show();
                mMainActivity.hideProgressDialog();
                Intent intent = new Intent(mMainActivity, ShakeActivity.class);
                mMainActivity.startActivity(intent);
                mMainActivity.finish();
            }

            @Override
            public void bleConnectFaild() {
                mMainActivity.hideProgressDialog();
                Toast.makeText(mMainActivity, "连接失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
